#!/bin/bash
if [ "$#" -lt 2 ]; then
    echo "Usage: $0 [mount|umount] <local_mount_point> <user_email:password@webdav_address>"
    exit 1
fi
action=$1
local_mount_point=$2
echo "Action: $action"
echo "Local Mount Point: $local_mount_point"

if [ "$action" == "umount" ]; then
    sudo umount "$local_mount_point"
    if [ $? -eq 0 ]; then
        echo "UnMounted successfully!"
    else
        echo "Error: UnMounting failed."
    fi
elif [ "$action" == "mount" ]; then
    # 检查参数数量
    if [ "$#" -ne 3 ]; then
        echo "Usage: $0 [mount|umount] <local_mount_point> <user_email:password@webdav_address>"
        exit 1
    fi
    user_info=$3
    IFS=':' read -r user_email tmp <<< "$user_info"
    IFS='@' read -r password webdav_address <<< "$tmp"
    echo "User Email: $user_email"
    echo "Password: $password"
    echo "WebDAV Address: $webdav_address"
    
    echo "$password" | sudo mount -t davfs -o username="$user_email" "$webdav_address" "$local_mount_point"
    if [ $? -eq 0 ]; then
        echo "Mounted successfully!"
    else
        echo "Error: Mounting failed."
    fi
else
    echo "Invalid action. Please use 'mount' or 'umount'."
    exit 1
fi

echo ""